#include "luteco/lut.h"
#include "util/util.h"
#include "base/cktname.h"
#include <cstdio>
#include <cstdlib>

static bool inside( unsigned idx , const vector<unsigned> & vec ){
   for( unsigned i = 0 ; i < vec.size() ; ++i ) 
      if( vec[i] == idx ) return true;  
   return false ;
}

int main( int argc , const char ** argv )
{
   ArgAnalyzer arg( "modify <in:bench> <out:bench> [-seed <int:seed>] [-num <int:num>]" , argc , argv );
   
   FPGA ckt;
   CktName name;
   ckt.read_bench( arg[0].c_str() , & name );

   unsigned seed = time(0);
   if( arg["-seed"].setted ) seed = atoi( arg["-seed"][0] );
   cout << "seed " << seed << endl;
   srand( seed );

   unsigned num = 1;
   if( arg["-num"].setted ) num = atoi( arg["-num"][0] );
   if( num >= (ckt.size() - ckt.andFlag) ) num = ckt.size() - ckt.andFlag ;
   cout << num << endl; 

   vector<unsigned> selected;

   while( selected.size() != num )
   {
      unsigned idx = rand() % ( ckt.size() - ckt.andFlag );
      string n = name.nodeName[ idx ];
      unsigned f  = rand() & 0xFFFF ;
      idx += ckt.andFlag; 
      if( inside( idx , selected ) ) continue;
      selected.push_back( idx );
      cout << idx << " " << n << " " << std::hex << ckt[idx].init << " " << f << std::dec << endl;
      ckt[ idx ].init = f;
   }

   ckt.write_bench ( arg[1].c_str() , & name );
   return 0;
}
